#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define lc (o<<1)
#define rc (o<<1|1)
#define mid ((l+r)>>1)
#define fir first
#define sec second
#define mkp make_pair
using namespace std;
typedef long long LL;
typedef double db;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+ch-'0'; ch=getchar(); }
	return x*f;
}
const int MAXN=5e5+10;
int n,m,head[MAXN*10],cnt,tot,S,pos[MAXN],dis[MAXN*10];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q;
struct edge
{
	int v,next,val;
}e[MAXN*60];
void addedge(int x,int y,int z)
{
	e[++cnt]=(edge){y,head[x],z};
	head[x]=cnt;
	return;
}
struct Seg_T
{
	void build(int o,int l,int r)
	{
		if(l==r)
		{
			pos[l]=o;
			return addedge(o+(n<<2),o,0);
		}
		build(lc,l,mid);
		build(rc,mid+1,r);
		addedge(lc,o,0);addedge(rc,o,0);addedge(o+(n<<2),lc+(n<<2),0);addedge(o+(n<<2),rc+(n<<2),0);
		return;
	}
	void update(int o,int l,int r,int yl,int yr,bool flag)
	{
		if(yl<=l&&r<=yr)
		{
			if(!flag)addedge(o,tot,1);
			else addedge(tot,o+(n<<2),1);
			return;
		}
		if(yl<=mid)update(lc,l,mid,yl,yr,flag);
		if(yr>mid)update(rc,mid+1,r,yl,yr,flag);
		return;
	}
}T1;
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	n=read();m=read();S=read();
	T1.build(1,1,n);
	tot=n<<3;
	for(int i=1;i<=m;++i)
	{
		int x1=read(),y1=read(),x2=read(),y2=read();
		++tot;T1.update(1,1,n,x1,y1,false);T1.update(1,1,n,x2,y2,true);
		++tot;T1.update(1,1,n,x2,y2,false);T1.update(1,1,n,x1,y1,true);
	}
	q.push(mkp(0,pos[S]));memset(dis,INF,sizeof(dis));dis[pos[S]]=0;
	while(!q.empty())
	{
		int u=q.top().sec;q.pop();
		for(int i=head[u];i;i=e[i].next)
		{
			int v=e[i].v;
			if(dis[v]>dis[u]+e[i].val)
			{
				dis[v]=dis[u]+e[i].val;
				q.push(mkp(dis[v],v));
			}
		}
	}
	for(int i=1;i<=n;++i)printf("%d\n",dis[pos[i]]>>1);
	return 0;
}
